/**
 * 文件：UserDAOImpl.java
 * 作者：朱娜
 * 完成度：完成
 * 最后修改日期：2023-7-4 18：45
 */
//修改完成
package com.gomokult.dao.impl;
import com.gomokult.dao.UserDAO;
import com.gomokult.entity.User;
import com.gomokult.exception.IllegalOperationException;
import com.gomokult.exception.InvalidIDException;
import com.gomokult.exception.UserRegisteredException;
import java.sql.ResultSet;
import java.sql.SQLException;
import static com.gomokult.utility.JDBCUtil.con;
import static com.gomokult.utility.JDBCUtil.pstat;

public class UserDAOImpl implements UserDAO {
	private UserDAOImpl() {
	}

	private static final UserDAOImpl singleton = new UserDAOImpl();

	public static UserDAOImpl getSingleton() {
		return singleton;
	}

	@Override
	public int insertrUser(String email, String nickname, String password) {//修改完
		try {
			String sql1 = "select * from  users where email = ?";
			pstat = con.prepareStatement(sql1);
			pstat.setString(1, email);
			ResultSet rs1 = pstat.executeQuery();
			if (!rs1.next()) {
				String sql2 = "insert into users(nickname,password,profile,email,score,winGames,totalGames) values(?,?,?,?,?,?,?)";
				pstat = con.prepareStatement(sql2);
				pstat.setString(1, nickname);
				pstat.setString(2, password);
				pstat.setString(3, "None");
				pstat.setString(4, email);
				pstat.setInt(5, 0);
				pstat.setInt(6, 0);
				pstat.setInt(7, 0);
				pstat.executeUpdate();
				int a = selectUser(email, password);//调用函数查询id
				return a;//返回查询的id
			} else {
				return 0;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	@Override
	public int selectUser(String email, String password) {//正确
		try {
			String sql = "select * from  users where email = ? and password = ?";
			pstat = con.prepareStatement(sql);
			pstat.setString(1, email);
			pstat.setString(2, password);
			ResultSet rs = pstat.executeQuery();
			if (rs.next()) {
				return rs.getInt("userID");
			} else {
				return 0;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	@Override
	public User  selectUserbyID(int userID) {//正确
		try {
			String sql = "select * from  users where userID = ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1, userID);
			ResultSet rs = pstat.executeQuery();
			if (rs.next()) {
				User user = new User(rs.getInt("userID"), rs.getString("nickname"), null,
						rs.getString("profile"), rs.getString("email"), rs.getInt("score"),
						rs.getInt("winGames"), rs.getInt("totalGames"));
				return user;
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	@Override
	public void updateUser(int executorID, User pattern) throws InvalidIDException, IllegalOperationException, UserRegisteredException {
		try {//修改完
			if (executorID != pattern.getUserID()) {
				throw new IllegalOperationException();
			}
			String sql1 = "select * from  users where email = ?";
			pstat = con.prepareStatement(sql1);
			pstat.setString(1, pattern.getEmail());
			ResultSet rs1 = pstat.executeQuery();
			if (!rs1.next()) {//查询是否存在此用户
				throw new UserRegisteredException();
			}
			String sql3 = "update users set nickname = ? , profile = ? , email = ? ,score = ? where userId = ?";
			pstat = con.prepareStatement(sql3);
			pstat.setString(1, pattern.getNickName());
			pstat.setString(2, pattern.getProfile());
			pstat.setString(3, pattern.getEmail());
			pstat.setInt(4, pattern.getScore());
			pstat.setInt(5,pattern.getUserID());
			pstat.executeUpdate();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void deleteUser(int userID) throws InvalidIDException {//修改完
		try {
			String sql1 = "select * from users where userID = ?";//查询该用户是否存在
			pstat = con.prepareStatement(sql1);
			pstat.setInt(1, userID);
			ResultSet rs1 = pstat.executeQuery();
			if (rs1.next()) {
				String sql2 = "delete  from users where userID = ?";
				pstat = con.prepareStatement(sql2);
				pstat.setInt(1, userID);
				pstat.executeUpdate();
			} else {
				throw new InvalidIDException();
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User selectUserbyEmail(String email) {
		try {
			String sql = "select * from  users where email = ?";
			pstat = con.prepareStatement(sql);
			pstat.setString(1, email);
			ResultSet rs = pstat.executeQuery();
			if (rs.next()) {
				User user = new User(rs.getInt("userID"), rs.getString("nickname"), null,
						rs.getString("profile"), rs.getString("email"), rs.getInt("score"),
						rs.getInt("winGames"), rs.getInt("totalGames"));
				return user;
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int selectBestscore() {
		try {
			String sql = "select score from  users order by score DESC";
			pstat = con.prepareStatement(sql);
			ResultSet rs = pstat.executeQuery();
			if (rs.next()) {
				int score = rs.getInt("score");
				return score;
			}
			return 0;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}



